package com.termux.tasker.utils;


//import com.termux.tasker.ResourceTable;
import com.termux.tasker_ohos.ResourceTable;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.agp.window.dialog.ToastDialog;

import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;


public class Logger {

    public static final int MY_MODULE = 0;
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, "MY_TAG");

    public static final String DEFAULT_LOG_TAG = "termux-tasker";

    public static final int LOG_LEVEL_OFF = 0; // log nothing
    public static final int LOG_LEVEL_NORMAL = 1; // start logging error, warn and info messages and stacktraces
    public static final int LOG_LEVEL_DEBUG = 2; // start logging debug messages
    public static final int LOG_LEVEL_VERBOSE = 3; // start logging verbose messages

    public static final int DEFAULT_LOG_LEVEL = LOG_LEVEL_NORMAL;

    static public void logMesssage(Context context, int logLevel, String tag, String message) {
        if (context == null) { return; }

        int currentLogLevel = getLogLevel(context);

        if (logLevel == HiLog.ERROR && currentLogLevel >= LOG_LEVEL_NORMAL)
        { HiLog.error(LABEL, tag, message); }
        else if (logLevel == HiLog.WARN && currentLogLevel >= LOG_LEVEL_NORMAL)
        { HiLog.warn(LABEL, tag, message); }
        else if (logLevel == HiLog.INFO && currentLogLevel >= LOG_LEVEL_NORMAL)
        { HiLog.info(LABEL, tag, message); }
        else if (logLevel == HiLog.DEBUG && currentLogLevel >= LOG_LEVEL_DEBUG)
        { HiLog.debug(LABEL, tag, message); }
        else if (logLevel == HiLog.LOG_APP && currentLogLevel >= LOG_LEVEL_VERBOSE)
        { HiLog.fatal(LABEL, tag, message); }
    }

    static public void logError(Context context, String tag, String message) {
        logMesssage(context, HiLog.ERROR, tag, message);
    }

    static public void logError(Context context, String message) {
        logMesssage(context, HiLog.ERROR, DEFAULT_LOG_TAG, message);
    }

    static public void logWarn(Context context, String tag, String message) {
        logMesssage(context, HiLog.WARN, tag, message);
    }

    static public void logWarn(Context context, String message) {
        logMesssage(context, HiLog.WARN, DEFAULT_LOG_TAG, message);
    }

    static public void logInfo(Context context, String tag, String message) {
        logMesssage(context, HiLog.INFO, tag, message);
    }

    static public void logInfo(Context context, String message) {
        logMesssage(context, HiLog.INFO, DEFAULT_LOG_TAG, message);
    }

    static public void logDebug(Context context, String tag, String message) {
        logMesssage(context, HiLog.DEBUG, tag, message);
    }

    static public void logDebug(Context context, String message) {
        logMesssage(context, HiLog.DEBUG, DEFAULT_LOG_TAG, message);
    }

    static public void logVerbose(Context context, String tag, String message) {
        logMesssage(context, HiLog.LOG_APP, tag, message);
    }

    static public void logVerbose(Context context, String message) {
        logMesssage(context, HiLog.LOG_APP, DEFAULT_LOG_TAG, message);
    }

    static public void logErrorAndShowToast(Context context, String tag, String message) {
        if (context == null) { return; }

        if (getLogLevel(context) >= LOG_LEVEL_NORMAL) {
            logError(context, tag, message);
            showToast(context, message);
        }
    }

    static public void logErrorAndShowToast(Context context, String message) {
        logErrorAndShowToast(context, DEFAULT_LOG_TAG, message);
    }

    static public void logDebugAndShowToast(Context context, String tag, String message) {
        if (context == null) { return; }

        if (getLogLevel(context) >= LOG_LEVEL_DEBUG) {
            logDebug(context, tag, message);
            showToast(context, message);
        }
    }

    static public void logDebugAndShowToast(Context context, String message) {
        logDebugAndShowToast(context, DEFAULT_LOG_TAG, message);
    }

    static public void logStackTrace(Context context, String tag, String message, Exception e) {
        if (context == null) { return; }

        if (getLogLevel(context) >= LOG_LEVEL_NORMAL) {
            try {
                StringWriter errors = new StringWriter();
                PrintWriter pw = new PrintWriter(errors);
                e.printStackTrace(pw);
                pw.close();
                if (message != null)
                { HiLog.error(LABEL, tag, message + ":\n" + errors.toString()); }
                else
                { HiLog.error(LABEL, tag, errors.toString()); }
                errors.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    static public void logStackTrace(Context context, String tag, Exception e) {
        logStackTrace(context, tag, null, e);
    }

    static public void logStackTrace(Context context, Exception e) {
        logStackTrace(context, DEFAULT_LOG_TAG, null, e);
    }

    static public void showToast(final Context context, final String toastText) {
        if (context == null) { return; }
        Toast(context,toastText);
//        new ToastDialog(context)
//                .setText(toastText)
//                .setAlignment(LayoutAlignment.CENTER)
//                .show();
    }
    /**
     * 自定义toast 解决不换行问题
     *
     * @param string 弹出内容
     */
    static public void Toast(Context context,String string) {
        // 自定义弹框标题-因为原来的titleText文字过多不换行
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        // 获取text 标签
        Text title = (Text) toastLayout.findComponentById(ResourceTable.Id_toast_content);
        // 赋值
        title.setText(string);
        new ToastDialog(context)
                .setComponent(toastLayout)
                .setSize(MATCH_CONTENT, MATCH_CONTENT)
                .show();
    }

//    使用鸿蒙 Toastdialog
//    public static void showSetLogLevelDialog(final Context context) {
//        if (context == null) return;
//
//        ToastDialog builder = new ToastDialog(context);
//        builder.setText("Log Level");
//
//        String[] logLevels = {
//                getLogLevelLabel(context, LOG_LEVEL_OFF, true),
//                getLogLevelLabel(context, LOG_LEVEL_NORMAL, true),
//                getLogLevelLabel(context, LOG_LEVEL_DEBUG, true),
//                getLogLevelLabel(context, LOG_LEVEL_VERBOSE, true)
//        };
//
//        int currentLogLevel = getLogLevel(context);
//        builder.se (logLevels, currentLogLevel, (dialog, logLevel) -> {
//            switch (logLevel) {
//                case 0: setLogLevel(context, LOG_LEVEL_OFF); break;
//                case 1: setLogLevel(context, LOG_LEVEL_NORMAL); break;
//                case 2: setLogLevel(context, LOG_LEVEL_DEBUG); break;
//                case 3: setLogLevel(context, LOG_LEVEL_VERBOSE); break;
//                default: break;
//            }
//            dialog.onDestroy();
//        });
//
//        AlertDialog dialog = builder.create();
//        dialog.show();
//    }


    static public int getLogLevel(Context context) {
        return QueryPreferences.getLogLevel(context);
    }

    static public int getLogLevelFromFile(Context context) {
        return QueryPreferences.getLogLevelFromFile(context);
    }

    public static String getLogLevelLabel(final Context context, final int logLevel, final boolean addDefaultTag) {
        String logLabel;
        switch (logLevel) {
            case LOG_LEVEL_OFF:
                logLabel = "Off";
                break;
            case LOG_LEVEL_NORMAL:
                logLabel = "Normal";
                break;
            case LOG_LEVEL_DEBUG:
                logLabel = "Debug";
                break;
            case LOG_LEVEL_VERBOSE:
                logLabel = "Verbose";
                break;
            default:
                logLabel = "*Unknown*";
                break;
        }

        if (addDefaultTag && logLevel == DEFAULT_LOG_LEVEL)
        { return logLabel + " (default)"; }
        else
        { return logLabel; }
    }

    static public void setLogLevel(Context context, int logLevel) {
        QueryPreferences.setLogLevel(context, logLevel);
        String toastText="Logcat log level set to"+"\"%1$s\""+"for the plugin app and all plugin actions";
        showToast(context, String.format(toastText, getLogLevelLabel(context, getLogLevel(context), false)));
    }

}

